package com.qys.training.server.controller;

import com.qys.training.base.dto.BaseResult;
import com.qys.training.base.exception.QysException;
import com.qys.training.biz.auth.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
public class FilesController {

    public static final Logger logger = LoggerFactory.getLogger(FilesController.class);

    @Resource
    private FileService fileServiceImpl;

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public BaseResult upload(@RequestParam(value = "file", required = false) MultipartFile file) {
        if (file != null) {
            //获取文件名
            String oldName = file.getOriginalFilename();
            if (!StringUtils.isEmpty(oldName)) {
                String suffix = oldName.substring(oldName.lastIndexOf("."));
                if (!".pdf".equals(suffix)) {
                    return BaseResult.error("请上传pdf文件");
                }
            } else {
                //读不到文件名
                return BaseResult.error("未检测到文件");
            }
            try {
                fileServiceImpl.upload(file);
                return BaseResult.success("文件上传成功");
            } catch (QysException e) {
                return BaseResult.error("文件上传异常");
            }
        }
        return BaseResult.error("未检测到文件");
    }

    /**
     * 文件上传
     */
    @PostMapping("/update")
    public BaseResult update(Long id, @RequestParam(value = "file", required = false) MultipartFile file) {
        if (file != null) {
            //获取文件名
            String oldName = file.getOriginalFilename();
            if (!StringUtils.isEmpty(oldName)) {
                String suffix = oldName.substring(oldName.lastIndexOf("."));
                if (!".pdf".equals(suffix)) {
                    return BaseResult.error("请上传pdf文件");
                }
            } else {
                //读不到文件名
                return BaseResult.error("未检测到文件");
            }
            try {
                fileServiceImpl.update(id, file);
                return BaseResult.success("文件更新成功");
            } catch (QysException e) {
                if ("UPLOAD_ERROR".equals(e.getCode())) {
                    return BaseResult.error("文件更新异常");
                } else {
                    return BaseResult.error("无此ID的文件");
                }
            }
        }
        return BaseResult.error("未检测到文件");
    }


    /**
     * 文件下载
     */
    @GetMapping("/download/{id}")
    public BaseResult download(@PathVariable Long id, HttpServletResponse response) {
        try {
            fileServiceImpl.download(id, response);
        } catch (QysException e) {
            return BaseResult.error("无此ID的文件");
        }
        return BaseResult.success("下载成功");
    }

    /**
     * 文件删除
     */
    @GetMapping("/delete/{id}")
    public BaseResult download(@PathVariable Long id) {
        try {
            fileServiceImpl.delete(id);
        } catch (QysException e) {
            if ("DELETE_ERROR".equals(e.getCode())) {
                return BaseResult.error("删除失败");
            } else {
                return BaseResult.error("无此ID的文件");
            }
        }
        return BaseResult.success("删除成功");
    }

    /**
     * 文件查询
     */
    @GetMapping("/select")
    public BaseResult select(String fileName, String minSize, String maxSize, Integer currentPage, Integer pageSize) {
        return BaseResult.success(fileServiceImpl.select(fileName, minSize, maxSize, currentPage, pageSize));
    }

    /**
     * 传数组
     *
     * @param ids
     * @return
     */
    @PostMapping("/downloadZip")
    public void downloadZip(Long[] ids, HttpServletResponse response) {
        fileServiceImpl.downloadZip(ids, response);
    }


}
